package fr.dlachiche.gtasksync.auth;

import java.io.IOException;

import com.google.api.client.extensions.android2.AndroidHttp;
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.json.JsonHttpRequest;
import com.google.api.client.http.json.JsonHttpRequestInitializer;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.tasks.Tasks;
import com.google.api.services.tasks.TasksRequest;

import fr.dlachiche.gtasksync.activity.ClientCredentials;

public class TasksFactory {
	
	private static Tasks service = null;
	private final static HttpTransport transport = AndroidHttp.newCompatibleTransport();
	private static GoogleAccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(null);
	
	public static void init() {
		service = Tasks.builder(transport, new JacksonFactory()).setApplicationName("Google-TasksSample/1.0")
        .setHttpRequestInitializer(accessProtectedResource)
        .setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() {

          public void initialize(JsonHttpRequest request) throws IOException {
            TasksRequest tasksRequest = (TasksRequest) request;
            tasksRequest.setKey(ClientCredentials.KEY);
          }
        }).build();
	}
	
	public static Tasks getService() {
		return service;
	}
	
	public static GoogleAccessProtectedResource getResource() {
		return accessProtectedResource;
	}

}
